package com.yinhai.message.manage.register.dao;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.util.StringUtil;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.manage.register.po.EmailConfigPo;
import com.yinhai.message.manage.register.vo.EmailConfigVo;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author created by lanyc on 2017/1/4
 *
 */
public class EmailConfigDaoImpl extends HibernateDAO<EmailConfigPo> implements IEmailConfigDao {
		
	@Override
	public IPage<EmailConfigPo> queryAllAccountsByPage(TaParamDto dto,int start, int limit,String grid) {
		Map<String, Object> param = new HashMap<>();
		StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.register.vo.EmailConfigVo(")
        .append("ecp.adminId,")
        .append("ecp.emailAddress,")
        .append("ecp.password,")
        .append("ecp.sendHost,")
        .append("ecp.sid,")
        .append("ecp.systemId,")
        .append("ecp.systemName,")
        .append("ecp.sendPort,")
        .append("ecp.isSendSSL,")
        .append("ecp.isSendTLS,")
        .append("ecp.timeout,")
        .append("ecp.prefix,")
        .append("ecp.defaultemail) from com.yinhai.message.manage.register.po.EmailConfigPo ecp ")
				.append(" where 1=1 ");
		String emailAddress = dto.getAsString("qry_emailAddress");
        String systemName = dto.getAsString("qry_systemName");
        if(StringUtil.isNotEmpty(emailAddress)){
			hql.append(" and ecp.emailAddress like :emailAddress");
			param.put("emailAddress", "%"+emailAddress+"%");
        }
        if(StringUtil.isNotEmpty(systemName)){
			hql.append(" and ecp.systemName like :systemName");
			param.put("systemName", "%"+systemName+"%");
        }
        
		return selectFromMultiTableWithPage(hql.toString(), param, start, limit, grid);
	}

	public EmailConfigPo queryEmailCfgByEmailAddress(String emailAccount){
		List<EmailConfigPo> list = find("FROM EmailConfigPo ecp where ecp.emailAddress=? ",emailAccount);
		EmailConfigPo ecp = new EmailConfigPo();
		if(!ObjectUtils.isEmpty(list)){
			return ecp = list.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public Integer addEmailConfig(EmailConfigPo emailConfigPo) {
		save(emailConfigPo);
		return null;
	}

	@Override
	public Integer deleteEmailInfo(String emailAccount) {
		delete("DELETE FROM EmailConfigPo ecp where ecp.emailAddress=?",emailAccount);
		return null;
	}

	@Override
	public List<EmailConfigVo> queryAllEmailConfig() {
		StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.register.vo.EmailConfigVo(")
        .append("ecp.adminId,")
        .append("ecp.emailAddress,")
        .append("ecp.password,")
        .append("ecp.sendHost,")
        .append("ecp.sid,")
        .append("ecp.systemId,")
        .append("ecp.systemName,")
        .append("ecp.sendPort,")
        .append("ecp.isSendSSL,")
        .append("ecp.isSendTLS,")
        .append("ecp.timeout,")
        .append("ecp.prefix,")
        .append("ecp.defaultemail) from com.yinhai.message.manage.register.po.EmailConfigPo ecp ");
		 return super.selectFromMultiTable(hql.toString());
	}

	@Override
	public void updateEmailConfig(EmailConfigPo po) {
		StringBuilder hql = new StringBuilder("update EmailConfigPo po set");
		List<Object> objs = new LinkedList<>();
		if(po.getSystemId()!=null){
			hql.append(" po.systemId = ?,");
			objs.add(po.getSystemId());
		}
		if(po.getSid()!=null){
			hql.append(" po.sid = ?,");
			objs.add(po.getSid());
		}
		if(po.getSystemName()!=null){
			hql.append(" po.systemName = ?,");
			objs.add(po.getSystemName());
		}
		if(!ObjectUtils.isEmpty(po.getDefaultemail())){
			hql.append(" po.defaultemail = ?,");
			objs.add(po.getDefaultemail());
		}
		if(!ObjectUtils.isEmpty(po.getIsSendSSL())){
			hql.append(" po.isSendSSL = ?,");
			objs.add(po.getIsSendSSL());
		}
		if(!ObjectUtils.isEmpty(po.getIsSendTLS())){
			hql.append(" po.isSendTLS = ?,");
			objs.add(po.getIsSendTLS());
		}
		if(!ObjectUtils.isEmpty(po.getTimeout())){
			hql.append(" po.timeout = ?,");
			objs.add(po.getTimeout());
		}
		if(!ObjectUtils.isEmpty(po.getSendPort())){
			hql.append(" po.sendPort = ?,");
			objs.add(po.getSendPort());
		}
		if(!ObjectUtils.isEmpty(po.getPrefix())){
			hql.append(" po.prefix = ?,");
			objs.add(po.getPrefix());
		}
		if(!ObjectUtils.isEmpty(po.getPassword())){
			hql.append(" po.password = ?,");
			objs.add(po.getPassword());
		}
		if(!ObjectUtils.isEmpty(po.getSendHost())){
			hql.append(" po.sendHost = ?,");
			objs.add(po.getSendHost());
		}
		if(!ObjectUtils.isEmpty(po.getEmailAddress())){
			hql.append(" po.emailAddress = ?");
			objs.add(po.getEmailAddress());
		}
		if(!ObjectUtils.isEmpty(po.getEmailAddress())){
			hql.append(" where po.emailAddress = ?");
			objs.add(po.getEmailAddress());
		}
		update(hql.toString(),objs.toArray());
	}

	@Override
	public void removeDefault() {
		update("update EmailConfigPo ecp set ecp.defaultemail=0 where ecp.defaultemail=1");
	}

	@Override
	public EmailConfigVo queryDefaultEmail() {
		StringBuffer hql = new StringBuffer();
		hql.append("select new com.yinhai.message.manage.register.vo.EmailConfigVo(")
        .append("ecp.adminId,")
        .append("ecp.emailAddress,")
        .append("ecp.password,")
        .append("ecp.sendHost,")
        .append("ecp.sendPort,")
        .append("ecp.isSendSSL,")
        .append("ecp.isSendTLS,")
        .append("ecp.timeout,")
        .append("ecp.prefix,")
        .append("ecp.defaultemail) from com.yinhai.message.manage.register.po.EmailConfigPo ecp where ecp.defaultemail=1");
		return super.selectFiled(hql.toString());
	}
	
}
